{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Choosing an Executor for a Task (Electron)\n",
    "\n",
    "Within a lattice, every electron can use a different executor. These exectors can have different capabilities:  different hardware, different computation strategies, and so on.\n",
    "\n",
    "Executors are plugins. Any executor plugins found by the dispatcher are imported as classes in the `covalent.executor` name space.\n",
    "\n",
    "### Prerequisites\n",
    "\n",
    "1. [Start](./covalent_cli.rst) the Covalent server.\n",
    "\n",
    "2. Import `covalent`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import covalent as ct"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Procedure\n",
    "\n",
    "Initialize an executor with its class:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "executor1 = ct.executor.LocalExecutor()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2. Assign the executor to an electron using the keyword argument `executor`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "@ct.electron(executor=executor1)\n",
    "def identity(x):\n",
    "    return x"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Another way to specify an executor is by the short name of the executor plugin. \n",
    "\n",
    "In this case, the short name of the `LocalExecutor` is `local`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "@ct.electron(executor=\"local\")\n",
    "def square(x):\n",
    "    return x * x"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3. Construct rest of the workflow as usual within a `lattice`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "@ct.lattice\n",
    "def workflow(a):\n",
    "    val_1 = identity(x=a)\n",
    "    return square(x=val_1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4. Submit the workflow using the `dispatch` method:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5f0e220f-35dc-4a75-92c6-a00176f530e2\n"
     ]
    }
   ],
   "source": [
    "dispatch_id = ct.dispatch(workflow)(a=2)\n",
    "print(dispatch_id)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Dispatching the workflow generates a dispatch ID. \n",
    "\n",
    "Use this ID to query the status of the task and retrieve the results: "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Lattice Result\n",
      "==============\n",
      "status: COMPLETED\n",
      "result: 4\n",
      "input args: []\n",
      "input kwargs: {'a': '2'}\n",
      "error: None\n",
      "\n",
      "start_time: 2023-01-31 21:20:27.871124\n",
      "end_time: 2023-01-31 21:20:28.054424\n",
      "\n",
      "results_dir: /Users/mini-me/agnostiq/covalent/doc/source/how_to/execution/results\n",
      "dispatch_id: 5f0e220f-35dc-4a75-92c6-a00176f530e2\n",
      "\n",
      "Node Outputs\n",
      "------------\n",
      "identity(0): 2\n",
      ":parameter:2(1): 2\n",
      "square(2): 4\n",
      "\n"
     ]
    }
   ],
   "source": [
    "output = ct.get_result(dispatch_id=dispatch_id, wait=True)\n",
    "print(output)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### See Also\n",
    "\n",
    "[Getting the Results of an Electron Execution](../collection/query_electron_execution_result.ipynb)\n",
    "\n",
    "[Getting Results of Previous Runs](../collection/query_lattice_execution_result.ipynb)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
